<?php
class UsersController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list users
		$args = array(
			'keyword' => '%'.Yii::app()->request->getParam('keyword', null).'%',
			'role_id' => Yii::app()->request->getParam('role', null),
			'created_from' => Yii::app()->request->getParam('dfrom', null),
			'created_to' => Yii::app()->request->getParam('dto', null),
			'user_status' => null,
			'page' => $intPage,
			'limit' => $intRowsPerPage
		);
		$arrUsers = Users::getInstance()->getList($args);
		$intTotalRows = isset($arrUsers[0]['total_rows'])?$arrUsers[0]['total_rows']:0;
		
		$arrRoles = Roles::getInstance()->getList();
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('index', array(
			'arrUsers' => $arrUsers,
			'objPages' => $objPages,
			'arrRoles' => $arrRoles,
			'args' => $args
		));
	}
	
	public function actionForm()
	{
		$intUserId = Yii::app()->request->getParam('id', null);
		//update
		if(Yii::app()->request->isPostRequest)
		{
			$arrParams = Yii::app()->request->getParam('user', null);
			$arrParams['user_status'] = 1;
			
			//update user info
			if(empty($intUserId)){
				$intResult = Users::getInstance()->insert($arrParams);
				$intPostId = $intResult;
			}
			else{
				$intResult = Users::getInstance()->update($intUserId, $arrParams);
			}
			
			//udpate user roles
			$arrRoles = Yii::app()->request->getParam('roles', null); 
			if(!empty($arrRoles)){
				Roles::getInstance()->updateUserRole($intUserId, $arrRoles);
			}
			
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			$this->refresh();
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intUserId)){
			$arrItemInfo = Users::getInstance()->get($intUserId);
		}
		$arrRoles = Roles::getInstance()->getList();
		//render layout
		$this->render('form', array(
			'arrItemInfo' => $arrItemInfo,
			'arrRoles' => $arrRoles
		));
	}
	
	public function actionDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Users::getInstance()->delete($intId);
		header('Location: '.LINK_ADMINCP_USERS);
	}
	
	public function actionChangePassword()
	{
		if(Yii::app()->request->isPostRequest)
		{
			$objUser = Yii::app()->admin;
			$strOldPassword = Yii::app()->request->getParam('old_password', null);
			$strNewPassword = Yii::app()->request->getParam('new_password', null);
			//validate old password
			$isValid = Users::getInstance()->authenticate($objUser->getState('username'), $strOldPassword);
			if($isValid){
				//update new password
				Users::getInstance()->updatePassword($objUser->id, $strNewPassword);
				Yii::app()->admin->setFlash('ok', "Password changed successfully!");
				$this->refresh();
			}
			else{
				Yii::app()->admin->setFlash('error', "Old password wrong!");
			}
		}
		$this->render('change-password');
	}
	
	public function actionPermissions(){
		$intUserId = Yii::app()->request->getParam('id', null);
		$user = Users::getInstance()->get($intUserId);
		//load user roles
		$arrPermissions = Permissions::getInstance()->getUserPermissions($intUserId);
		if(Yii::app()->request->isPostRequest)
		{
			$arrCustomPermissions = Yii::app()->request->getParam('permissions', array());
			$arrDefaultPermissions = array();
			foreach($arrPermissions as $R){
				array_push($arrDefaultPermissions, $R['permission_id']);	
			}
			$arrDefaultPermissions = array_fill_keys($arrDefaultPermissions, 0);
			$arrCustomPermissions = array_fill_keys($arrCustomPermissions, 1);
			//merge 2 array of custome and default role's permissions
			$arrCustomPermissions = $arrCustomPermissions+$arrDefaultPermissions;
				
			//update db
			Permissions::getInstance()->updateUserPermissions($intUserId, $arrCustomPermissions);

			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			$this->refresh();
		}
		$this->render('permissions', array(
			'user' => $user,
			'arrPermissions' => $arrPermissions
		));	
	}

}